package com.splashscreen

import android.app.Activity
import android.app.Dialog
import android.os.Build
import android.view.WindowManager
import java.lang.ref.WeakReference

/**
 * SplashScreen
 * 启动屏
 * from：http://www.devio.org
 * Author:CrazyCodeBoy
 * GitHub:https://github.com/crazycodeboy
 * Email:crazycodeboy@gmail.com
 */
object SplashScreen {
    private var mSplashDialog: Dialog? = null
    private var mActivity: WeakReference<Activity>? = null

    /**
     * 打开启动屏
     */
    fun show(activity: Activity?, themeResId: Int, fullScreen: Boolean) {
        if (activity == null) return
        mActivity = WeakReference(activity)
        activity.runOnUiThread(Runnable {
            if (!activity.isFinishing) {
                mSplashDialog = Dialog(activity, themeResId)
                mSplashDialog!!.setContentView(R.layout.launch_screen)
                mSplashDialog!!.setCancelable(false)
                if (fullScreen) {
                    setActivityAndroidP(mSplashDialog)
                }
                if (!mSplashDialog!!.isShowing) {
                    mSplashDialog!!.show()
                }
            }
        })
    }
    /**
     * 打开启动屏
     */
    /**
     * 打开启动屏
     */
    @JvmOverloads
    fun show(activity: Activity?, fullScreen: Boolean = false) {
        val resourceId =
            if (fullScreen) R.style.SplashScreen_Fullscreen else R.style.SplashScreen_SplashTheme
        show(activity, resourceId, fullScreen)
    }

    /**
     * 关闭启动屏
     */
    fun hide(activity: Activity?) {
        var activity = activity
        if (activity == null) {
            if (mActivity == null) {
                return
            }
            activity = mActivity!!.get()
        }
        if (activity == null) return
        val _activity: Activity = activity
        _activity.runOnUiThread {
            if (mSplashDialog != null && mSplashDialog!!.isShowing) {
                var isDestroyed = false
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    isDestroyed = _activity.isDestroyed
                }
                if (!_activity.isFinishing && !isDestroyed) {
                    mSplashDialog!!.dismiss()
                }
                mSplashDialog = null
            }
        }
    }

    private fun setActivityAndroidP(dialog: Dialog?) {
        //设置全屏展示
        if (Build.VERSION.SDK_INT >= 28) {
            if (dialog != null && dialog.window != null) {
                dialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) //全屏显示
                val lp = dialog.window!!.attributes
                lp.layoutInDisplayCutoutMode =
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                dialog.window!!.attributes = lp
            }
        }
    }
}
